package com.maiyuren.fishkit.dict

import org.json.JSONObject

data class FishDictCfg(
    val id: String,
    val name: String,
    val path: String,
    var from: String?,
    var type: String?
) {
    init {
        if (from.isNullOrEmpty()) from = FishDictFrom.default
        if (type.isNullOrEmpty()) type = FishDictType.default
    }
    companion object {
        @JvmStatic
        fun fromMap(cfg: Map<String, Any>) : FishDictCfg{
            return FishDictCfg(
                id = cfg["id"] as String,
                name = cfg["name"] as String,
                path = cfg["path"] as String,
                type = cfg["type"] as String,
                from = cfg["from"] as String
            )
        }

        @JvmStatic
        fun fromJSON(cfg: JSONObject) : FishDictCfg{
            val id: String = try {cfg.getString("id")} catch (e: Exception){ "" }
            val name: String = try {cfg.getString("name")} catch (e: Exception){ "" }
            val path: String = try {cfg.getString("path")} catch (e: Exception){ "" }
            val type: String? = try {cfg.getString("type")} catch (e: Exception){ null }
            val from: String? = try {cfg.getString("from")} catch (e: Exception){ null }
            return FishDictCfg(
                id = id,
                name = name,
                path = path,
                type = type,
                from = from
            )
        }
    }
}
